package com.competitions.easydubbing.service.impl;


import com.competitions.easydubbing.dao.CommunityDao;
import com.competitions.easydubbing.entity.Result;
import com.competitions.easydubbing.entity.Video;
import com.competitions.easydubbing.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @author Mapler
 * @since 2021-5-20
 */
@Service
public class CommunityServiceImpl implements CommunityService {
    private final CommunityDao communityDao;

    @Autowired
    public CommunityServiceImpl(CommunityDao communityDao) {
        this.communityDao = communityDao;
    }

    @Override
    public ResponseEntity<Result> publishVideo(int uid, String title, String desc, String channel, MultipartFile cover, MultipartFile video) {
        try{
            String basePath = "C:\\uploadFiles\\";
            String coverPath = basePath+"images\\"+System.currentTimeMillis()+".png";
            String videoPath = basePath+"videos\\"+System.currentTimeMillis()+".mp4";
            File coverFile = new File(coverPath);
            File videoFile = new File(videoPath);
            cover.transferTo(coverFile);
            video.transferTo(videoFile);
            communityDao.insertVideo(uid, title, desc,
                    videoPath.replace("C:\\uploadFiles\\videos\\","http://47.102.150.68:8066/upload/videos/"),
                    coverPath.replace("C:\\uploadFiles\\images\\","http://47.102.150.68:8066/upload/images/"),
                    channel);
           return ResponseEntity.ok().body(Result.ok());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.status(500).body(Result.error("错误"));
        }
    }

    @Override
    public ResponseEntity<Result> publishDirectly(int uid, String title, String desc, String channel,String videoPath, MultipartFile cover) {
        try{
            String basePath = "C:\\uploadFiles\\";
            String coverPath = basePath+"images\\"+System.currentTimeMillis()+".png";
            File coverFile = new File(coverPath);
            cover.transferTo(coverFile);
            communityDao.insertVideo(uid, title, desc, videoPath,
                    coverPath.replace("C:\\uploadFiles\\images\\","http://47.102.150.68:8066/upload/images/"),
                    channel);
            return ResponseEntity.ok().body(Result.ok());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.status(500).body(Result.error("错误"));
        }
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    @Override
    public ResponseEntity<Result> deleteVideo(int vid) {
        Video video = communityDao.selectVideo(vid);
        if(video == null){
            return ResponseEntity.badRequest().body(Result.error("无此视频"));
        }
        File coverFile = new File(video.getCover().replace("http://47.102.150.68:8066/upload/images/","C:\\uploadFiles\\images\\"));
        File videoFile = new File(video.getUrl().replace("http://47.102.150.68:8066/upload/videos/","C:\\uploadFiles\\videos\\"));
        coverFile.delete();
        videoFile.delete();
        communityDao.deleteVideo(vid);
        return ResponseEntity.ok().body(Result.ok());
    }

    @Override
    public ResponseEntity<Result> changeNum(int vid,String field, int type,int uid) {
        switch (field) {
            case "applaud":
                communityDao.updateApplaud(vid,type);
                if(type == 1){
                    communityDao.insertNum(vid,uid,1);
                }else{
                    communityDao.deleteNum(vid,uid,1);
                }
                break;
            case "favor":
                communityDao.updateFavor(vid,type);
                if(type == 1){
                    communityDao.insertNum(vid,uid,2);
                }else{
                    communityDao.deleteNum(vid,uid,2);
                }
                break;
            case "play":
                communityDao.updatePlay(vid);
                break;
            default:
        }
        return ResponseEntity.ok().body(Result.ok());
    }

    @Override
    public ResponseEntity<Result> sendComment(int vid, String comment, int uid) {
        communityDao.insertComment(vid, uid, comment);
        communityDao.updateComment(vid);
        return ResponseEntity.ok().body(Result.ok());
    }

    @Override
    public ResponseEntity<Result> commentList(int vid) {
        return ResponseEntity.ok().body(Result.ok(communityDao.selectComments(vid)));
    }

    @Override
    public ResponseEntity<Result> findIndexList(int uid) {
        List<Map<String,Object>> dataList = communityDao.selectVideos(uid);
        return ResponseEntity.ok().body(Result.ok(dataList));
    }

    @Override
    public ResponseEntity<Result> findHistoryList(int uid) {
        List<Video> dataList = communityDao.selectVideosHistory(uid);
        return ResponseEntity.ok().body(Result.ok(dataList));
    }
}
